package com.ocom.xcxapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.XcxApiUrl;
import com.ocom.common.enums.ConsStateEnum;
import com.ocom.common.enums.OpTypeDiffEnum;
import com.ocom.common.enums.OpTypeEnum;
import com.ocom.common.enums.PayStateEnum;
import com.ocom.common.request.xcx.PayRequest;
import com.ocom.common.request.xcx.XcxPayListVo;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.simple.ConsumeDetailVo;
import com.ocom.xcxapi.client.XfRecordFeign;
import com.ocom.xcxapi.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2022/10/26
 **/
@Slf4j
@RestController

public class PayController {

	@Autowired
	XfRecordFeign xfRecordFeign;

	@Autowired
	private PayService payService;

	@PreAuthorize("hasRole('ROLE_XCX')")
	@PostMapping(XcxApiUrl.URL_payList)
	public Result getUserPayList(@RequestBody XcxPayListVo xcxPayListVo){
		JSONObject jsonObject =new JSONObject();
		jsonObject.put("comId",xcxPayListVo.getComId());
		jsonObject.put("pId",xcxPayListVo.getPId());
		jsonObject.put("page",xcxPayListVo.getPage());
		jsonObject.put("size",xcxPayListVo.getSize());
		jsonObject.put("startTime",xcxPayListVo.getStartTime());
		jsonObject.put("endTime",xcxPayListVo.getEndTime());
		Result result1 = xfRecordFeign.getConsumeDetail(jsonObject);

		ConsumeDetailVo mjRecords1 = JSON.parseObject(JSON.toJSONString(result1.getData()),ConsumeDetailVo.class);
		log.info("消费报表 【{}】",result1);
		if(mjRecords1 == null || mjRecords1.getList()==null){
			return ResultUtil.success(new ConsumeDetailVo());
		}
		for (int i =0 ; i<mjRecords1.getList().size();i++){
			Object o = mjRecords1.getList().get(i);
			JSONObject mjRecord= JSON.parseObject(JSON.toJSONString(o));
			mjRecord.put("chargeBalance",(double) (mjRecord.getInteger("chargeBalance")/100.0));
			mjRecord.put("chargeMoney",(double) (mjRecord.getFloatValue("chargeMoney")/100.0));
			mjRecord.put("consMoney",(double) (mjRecord.getFloatValue("consMoney")/100.0));
			mjRecord.put("consState",ConsStateEnum.getValueByKey(mjRecord.getIntValue("consState")));
			mjRecord.put("opType",OpTypeEnum.getName(mjRecord.getIntValue("opType")));
			mjRecord.put("opTypeDiff",OpTypeDiffEnum.getName(mjRecord.getIntValue("opTypeDiff")));
			mjRecord.put("payAmount",(double) (mjRecord.getFloatValue("payAmount")/100.0));
			mjRecord.put("payState",PayStateEnum.getValueByKey(mjRecord.getIntValue("payState")));
			mjRecord.put("realMoney",(double) (mjRecord.getFloatValue("realMoney")/100.0));
			mjRecord.put("subBalance",(double) (mjRecord.getFloatValue("subBalance")/100.0));
			mjRecord.put("subMoney",(double) (mjRecord.getFloatValue("subMoney")/100.0));
			mjRecord.put("subsidy",(double) (mjRecord.getFloatValue("subsidy")/100.0));
			mjRecords1.getList().set(i,mjRecord);
		}
		return ResultUtil.success(mjRecords1);

	}

	@PostMapping(XcxApiUrl.URL_PAY)
	public Result pay(PayRequest payRequest){
		return this.payService.pay(payRequest);
	}

}
